package cn.itsource.article.service.impl;


import cn.itsource.article.domain.Article;
import cn.itsource.article.mapper.ArticleMapper;
import cn.itsource.article.query.ArticleQuery;
import cn.itsource.article.service.ArticleService;
import cn.itsource.article.vo.ArticleVo;
import cn.itsource.param.domain.Like;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;


    /**
     * 增
     * @param a
     */
    @Override
    @Transactional
    public void insert(Article a) {
        if (a.getId()==null){
            articleMapper.insert(a);
        }else {
            System.out.println("用户已存在");
        }
    }



    /**
     * 改
     * @param a
     */
    @Override
    @Transactional
    public void updateByPrimaryKeySelective(Article a) {
        if (a.getId() != null) {
            articleMapper.updateByPrimaryKeySelective(a);
        }

    }


    /**
     * 分页和查询
     * @param ArticleQuery
     * @return
     */
    @Override
    public PageInfo<Article> pageInfo(ArticleQuery ArticleQuery) {
        PageHelper.startPage(ArticleQuery.getCurrentPage(), ArticleQuery.getPageSize());
        List<Article> all = articleMapper.getAll(ArticleQuery);
        return new PageInfo<Article>(all);
    }

    /**
     * 删除所有
     * @param a
     */
    @Override
    @Transactional
    public void deleteAll(List<Article> a) {
        articleMapper.deleteAll(a);
    }



    /**
     * 删除
     * @param id
     */
    @Override
    public int deleteByPrimaryKey(Long id) {
        return articleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ArticleVo> group() {
        return articleMapper.group();
    }

    @Override
    public List<ArticleVo> placeOnFile() {
        return articleMapper.placeOnFile();
    }

    @Override
    public Article selectId(Long articleId) {

        return articleMapper.selectByPrimaryKey(articleId);
    }

    @Override
    public List<Article> aboutArticles(Long articleId) {
        return articleMapper.aboutArticleId(articleId);
    }

    @Override
    public void like(Like like) {
        articleMapper.like(like);
    }

    @Override
    public void incrementReadCount(Long articleId) {
        articleMapper.findId(articleId);
    }

    @Override
    public List<Article> drawColumnChart() {
        List<Article> articleList = articleMapper.drawColumnChart();
        return articleList;
    }



}
